# -*- coding: utf-8 -*-
# @time: 2024/6/5 17:12
# @file: conversion_premium
# @author: tyshixi08

from get_data.origin_data import *

# 获取可转债转股溢价率
def get_conversion_premium(code=code_ls(), start_date = month_ls()[0].replace('-','') + '01', end_date = month_ls()[-1].replace('-','') + '01'):
    df = convertible.get_indicators(code,start_date, end_date,fields='conversion_premium').reset_index()
    return df

# 获取可转债转股溢价率中位数时间序列
def conversion_premium_median(code=code_ls(), start_date = month_ls()[0].replace('-','') + '01', end_date = month_ls()[-1].replace('-','') + '01'):
    df = get_conversion_premium(code, start_date, end_date)
    df_conversion_premium_median = df.groupby('date')['conversion_premium'].median().reset_index().rename(columns={'conversion_premium':'covpremium_mid'})
    return df_conversion_premium_median.sort_values('date')

# 数据存档
def covpremium_mid_save():
    excel_file_path = 'covpremium_mid.csv'
    #if os.path.exists('原始数据/' + excel_file_path):
    #    df = pd.read_csv('原始数据/' + excel_file_path)
    #    return df
    #else:
    save_CSV(conversion_premium_median(), 'get_data/原始数据/' + excel_file_path.split('.')[0])
    df = pd.read_csv('get_data/原始数据/' + excel_file_path)
    return df
